/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import cn.webrx.mapper.StudentMapper;
import cn.webrx.mybatis.interceptor.MyPager;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * <p>Project: mybatis2021 - MyPageTest
 * <p>Powered by webrx On 2021-07-15 10:01:54
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
public class MyPageTest {
    public static void main(String[] args) throws IOException {
        //建立数据源，
        DruidDataSource ds = new DruidDataSource();
        Properties prop = new Properties();
        prop.load(Resources.getResourceAsReader("db.properties"));
        ds.setConnectProperties(prop);

        TransactionFactory tx = new JdbcTransactionFactory();
        Environment env = new Environment("development", tx, ds);
        Configuration cfg = new Configuration(env);

        cfg.addMappers("cn.webrx.mapper");
        //添加拦截器插件
        PageInterceptor pi = new PageInterceptor();
        cfg.addInterceptor(pi);

        var mp = new MyPager();
        prop.setProperty("currpage", "1");
        prop.setProperty("pagesize", "3");
        mp.setProperties(prop);
        cfg.addInterceptor(mp);

        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
        SqlSession ss = sf.openSession();

        StudentMapper sm = ss.getMapper(StudentMapper.class);
        //PageHelper.startPage(1, 3);
        //PageInfo pp = PageInfo.of(sm.query());
        //System.out.println(pp);

        //MyPager.startPage(3);
        //MyPager.startPage(1,5);
        //默认1,3
        System.out.println(sm.myByPage());
        //limit order by

    }
}
